/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package redinftel.servlets;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import redinftel.entidades.Multimedia;
import redinftel.entidades.Usuario;
import redinftel.sesiones.MultimediaFacadeRemote;

/**
 *
 * @author inftel
 */
public class SubirMultimediaServlet extends HttpServlet {

    @EJB
    private MultimediaFacadeRemote multimediaFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {

            System.out.println("entra");
            int tamano = Integer.parseInt(request.getParameter("tamano"));
            String idUsuario = request.getParameter("idUsuario");
            String nombre = request.getParameter("nombre");
            nombre = URLDecoder.decode(nombre, "UTF-8");
            // Obtengo la imagen
            Part p = request.getPart("imagen");
            InputStream is = p.getInputStream();
            byte[] imag = new byte[(int) p.getSize()];
            if (imag.length > 0) {
                is.read(imag);
                is.close();
            }
            Multimedia multimedia = new Multimedia();
            multimedia.setImagen(imag);
            multimedia.setTipo("imagen/jpg");
            multimedia.setTitulo(nombre);
            multimedia.setFecha(new Date());
            multimedia.setNombre(nombre);
            Usuario us = new Usuario(new BigDecimal(idUsuario));
            multimedia.setUsuario(us);
            multimedia.setIdMultimedia(multimediaFacade.findPK());
            multimediaFacade.create(multimedia);
            System.out.println("guarda");
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
